<?php
$loader->requireOnce('lib/OFC/OFC_Chart.php');

abstract class Portlet {
	protected $name;
	protected $values;
	protected $xLabels;
	protected $yLabels;
	protected $chart;
	protected $dateRange;
	protected $_db;
	protected $startDate;
	protected $endDate;
	
	public function renderChart() {
		$this->endDate = date('Y-m-d');
		if ($this->dateRange > 0) {
			$this->startDate = $this->getLowerLimitDate($this->dateRange);
		} else {
			$this->startDate = $this->getMonthStartDate($this->endDate);
		}
		$this->collectData();
		$this->buildChart();
		return $this->chart->toPrettyString();
	}
	
	abstract protected function collectData();
	abstract protected function buildChart();
	
	protected function getLowerLimitDate($dayRange) {
		$now = mktime();
		return date('Y-m-d', $now - 86400*$dayRange); 
	}
	
	protected function getShortDate($isoDate, $delimiter = " / ") {
		$dateStr = L10nHelper::renderDateTime($isoDate);
		$datePar = split('[/.-]',$dateStr);
		return $datePar[0] . $delimiter . $datePar[1];
	}
	
	protected function getWeekDay($isoDate) {
		$date = explode("-", $isoDate);
		$dateInfo = getdate(mktime(0, 0, 0, $date[1], $date[2], $date[0]));
		$dateStr = L10nHelper::renderDateTime($isoDate);
		$datePar = split('[/.-]',$dateStr);
		return ($dateInfo['wday'] == 0) ? L10nHelper::_('Sunday')." (" . $datePar[0] . "/" . $datePar[1] .")" : Utils::getDateMap($dateInfo['wday'] + 1) . " (" . $datePar[0] . "/" . $datePar[1] .")";
	}
	
	protected function getMonthStartDate($isoDate) {
		$date = explode("-", $isoDate);
		return date('Y-m-d', mktime(0, 0, 0, $date[1], 1, $date[0]));
	}
	
}
?>